package com.meals.system.service.impl;

import com.meals.system.api.system.vo.WhitesMealSearchGoodsVO;
import com.meals.system.api.system.vo.WhitesMealSearchSellerVO;
import com.meals.system.dao.SellerInfoMapper;
import com.meals.system.entity.SystemSellerInfo;
import com.meals.system.service.WhitesMealSellerInfoService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.PageQueryUtil;
import com.meals.system.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/18
 */
@Service
public class WhitesMealSellerInfoServiceImpl implements WhitesMealSellerInfoService {

    @Autowired
    private SellerInfoMapper sellerInfoMapper;


    @Override
    public PageResult searchWhitesMealSeller(PageQueryUtil pageUtil){
        // 查询到的结果
        List<SystemSellerInfo> sellerInfoList = sellerInfoMapper.findSellerInfoListBySearch(pageUtil);
        int total = sellerInfoMapper.getTotalWhitesMealSellersBySearch(pageUtil);
        List<WhitesMealSearchSellerVO> whitesMealSearchSellerVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sellerInfoList)){
            whitesMealSearchSellerVOS = BeanUtil.copyList(sellerInfoList, WhitesMealSearchSellerVO.class);
            for (WhitesMealSearchSellerVO whitesMealSearchSellerVO : whitesMealSearchSellerVOS){
                String sellerName = whitesMealSearchSellerVO.getSellerName();
                String sellerIntro = whitesMealSearchSellerVO.getSellerIntro();
                // 字符串过长导致文字超出的问题
                if (sellerName.length() > 28){
                    sellerName = sellerName.substring(0,28) + "...";
                    whitesMealSearchSellerVO.setSellerName(sellerName);
                }
                if (sellerIntro.length() > 30){
                    sellerIntro = sellerIntro.substring(0,30) + "...";
                    whitesMealSearchSellerVO.setSellerIntro(sellerIntro);
                }
            }
        }
        PageResult pageResult = new PageResult(whitesMealSearchSellerVOS, total, pageUtil.getLimit(),pageUtil.getPage());
        return pageResult;
    }

    @Override
    public SystemSellerInfo getWhitesMealSellerById(Long sellerId){
        return sellerInfoMapper.selectByPrimaryKey(sellerId);
    }

}
